//
//  WYTPrivateVC.m
//  WuyouTang
//
//  Created by yunbao02 on 2024/8/26.
//  Copyright © 2024 cat. All rights reserved.
//

#import "WYTPrivateVC.h"

#import "WYTPrivateCell.h"

@interface WYTPrivateVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray *dataList;
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation WYTPrivateVC


- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0,*)) {
        return UIStatusBarStyleDarkContent;
    }
    return UIStatusBarStyleDefault;
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self pulldata];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleL.text = @"隐私设置";
    self.titleL.textColor = UIColor.blackColor;
    self.view.backgroundColor = RGB_COLOR(@"#f3f3f3", 1);
    [self.leftBtn setImage:[UIImage imageNamed:@"pub_back_black"] forState:0];
    self.naviLine.hidden = YES;
    self.subNavi.backgroundColor = UIColor.clearColor;
    
    _dataList = [NSMutableArray array];
    
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0,*)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
}

-(void)pulldata {
    [WYNetworking postWithUrl:@"User.getPrivateSetting" Dic:@{} Suc:^(int code, id info, NSString *msg) {
        if (code==0) {
            NSDictionary *infoDic;
            if ([PublicObj isArray:info]) {
                infoDic = [NSDictionary dictionaryWithDictionary:[info firstObject]];
            }else{
                infoDic = [NSDictionary dictionaryWithDictionary:info];
            }
            [self dataDeal:infoDic];
        }else {
            [self dataDeal:@{}];
        }
    } Fail:^(id fail) {
        [self dataDeal:@{}];
    }];
}

-(void)dataDeal:(NSDictionary *)infoDic {
    [_dataList removeAllObjects];
    int like_switch = [strFormat([infoDic valueForKey:@"like_switch"]) intValue];
    int collect_switch = [strFormat([infoDic valueForKey:@"collect_switch"]) intValue];
    NSDictionary *likeDic = @{
        @"event":@"like",
        @"icon":@"p_like",
        @"name":@"喜欢",
        @"val":@(like_switch),
    };
    [_dataList addObject:likeDic];
    NSDictionary *collectionDic = @{
        @"event":@"collection",
        @"icon":@"p_collection",
        @"name":@"收藏",
        @"val":@(collect_switch),
    };
    [_dataList addObject:collectionDic];
    [_tableView reloadData];
}

#pragma mark - UITableViewDelegate、UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WYTPrivateCell *cell = [WYTPrivateCell cellWithTab:tableView index:indexPath];
    cell.dataDic = _dataList[indexPath.row];
    if (indexPath.row + 1 == _dataList.count) {
        cell.lineL.hidden = YES;
    }else {
        cell.lineL.hidden = NO;
    }
    YBWeakSelf;
    cell.stateEvent = ^{
        [weakSelf pulldata];
    };
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - set/get
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,naviHight, _window_width, _window_height - naviHight)style:UITableViewStyleInsetGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UIColor.clearColor;
    }
    return _tableView;
}

@end
